using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Collections;

namespace miciv
{
    public class EntityPrototype
    {
        protected string m_strName;
        protected Hashtable m_htblComponents;
        protected CoreContext m_coreContext;
        protected CoreManager m_coreManager;
        protected List<string> m_lstTags;

        public EntityPrototype(XmlElement _xmlConfig, CoreContext _coreContext)
        {
            this.m_coreContext = _coreContext;
            this.m_coreManager = this.m_coreContext.CoreManager;
            this.m_htblComponents = new Hashtable();
            this.m_strName = _xmlConfig.Attributes["name"].Value;
            this.m_lstTags = new List<string>();

            XmlNodeList lstAttrTags = _xmlConfig.SelectNodes("tags/tag/@id");
            foreach (XmlAttribute attrID in lstAttrTags)
            {
                if (false == this.m_lstTags.Contains(attrID.Value))
                {
                    this.m_lstTags.Add(attrID.Value);
                }
            }

            XmlElement xmlComponents = _xmlConfig.SelectSingleNode("components") as XmlElement;
            if (null != xmlComponents)
            {
                foreach (XmlNode xmlChild in xmlComponents.ChildNodes)
                {
                    XmlElement xmlComponent = xmlChild as XmlElement;
                    if (null != xmlComponent)
                    {
                        if (false != this.m_htblComponents.ContainsKey(xmlComponent.Name))
                        {
                            throw new SystemException(xmlComponent.Name + " component already declared (" + this.m_strName + " entity)");
                        }
                        IComponentPrototype coProto = this.m_coreManager.CreateComponentPrototype(this, xmlComponent);
                        this.m_htblComponents[xmlComponent.Name] = coProto;
                    }
                }
            }
        }

        virtual public string Name
        {
            get
            {
                return this.m_strName;
            }
        }

        virtual public Hashtable Components
        {
            get
            {
                return this.m_htblComponents;
            }
        }

        virtual public CoreContext CoreContext
        {
            get
            {
                return this.m_coreContext;
            }
        }

        virtual public CoreManager CoreManager
        {
            get
            {
                return this.m_coreManager;
            }
        }

        virtual public bool HasTag(string _strTag)
        {
            return this.m_lstTags.Contains(_strTag);
        }
    }
}
